﻿@model MessageTemplateListModel
@using Telerik.Web.Mvc.UI
@using SmartStore.Web.Framework;
@{
    ViewBag.Title = T("Admin.ContentManagement.MessageTemplates").Text;
}
<div class="section-header">
    <div class="title">
        <i class="far fa-envelope"></i>
        @T("Admin.ContentManagement.MessageTemplates")
    </div>
    <div class="options">
		@{ Html.RenderWidget("admin_button_toolbar_before"); }

        @{ Html.RenderWidget("admin_button_toolbar_after"); }
    </div>
</div>

@if (Model.AvailableStores.Count > 1)
{
	<div class="row mt-3 grid-filter">
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.SearchStoreId)
			@Html.DropDownList("SearchStoreId", Model.AvailableStores, T("Admin.Common.All"))
		</div>

		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabel(string.Empty, string.Empty)
			<button type="submit" id="btnSearch" value="btnSearch" class="btn btn-secondary btn-block">
				<span>@T("Admin.Common.ApplyFilter")</span>
			</button>
		</div>
	</div>
}

<div>
	@(Html.Telerik().Grid<MessageTemplateModel>()
			.Name("templates-grid")
			.Columns(columns =>
			{
				columns.Bound(x => x.Name)
					.Template(x => Html.ActionLink(x.Name, "Edit", new { id = x.Id }))
					.ClientTemplate("<a href=\"Edit/<#= Id #>\"><#= Name #></a>");
				columns.Bound(x => x.Subject);
				columns.Bound(x => x.IsActive)
					.Template(item => @Html.SymbolForBool(item.IsActive))
					.ClientTemplate(@Html.SymbolForBool("IsActive"))
					.Centered();
				columns.Bound(x => x.SendManually)
					.Template(item => @Html.SymbolForBool(item.SendManually))
					.ClientTemplate(@Html.SymbolForBool("SendManually"))
					.Centered();
			})
			.DataBinding(dataBinding => dataBinding.Ajax().Select("List", "MessageTemplate"))
			.ClientEvents(events => events.OnDataBinding("onDataBinding").OnRowDataBound("onRowDataBound"))
			.EnableCustomBinding(true))
</div>

<script type="text/javascript">
    $(document).ready(function () {
        //search button
		$('#btnSearch').click(function () {
            //search
            var grid = $('#templates-grid').data('tGrid');
            //no paging support in this grid
            //grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
    });

    function onDataBinding(e) {
        var searchModel = {
            SearchStoreId: $('#SearchStoreId').val()
        };
        e.data = searchModel;
    }

    function onRowDataBound(e) {
    	if (!e.dataItem.IsActive)
    		$(e.row).find('td').wrapInner('<span class="muted" />');
    }
</script>
